﻿using System;
using System.Collections.Generic;
using System.Linq;
using NFine.Code;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.SystemManage;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;
using NFine.Repository.SystemManage;

namespace NFine.Application.Biz
{
    public class IntegralDetailApp
    {
        IIntegralDetailRepository integralDetailRepository = new IntegralDetailRepository();
        IUserRepository userRepository = new UserRepository();

        public List<IntegralDetailEntity> GetListForIntegral(Pagination pagination, string memberId, int IntegralType, string start, string end)
        {
            var expression = ExtLinq.True<IntegralDetailEntity>();
            DateTime date = DateTime.Now;

            
            if (IntegralType != -1)
            {
                expression = expression.And(a => a.F_Type == IntegralType);
            }
            if (!string.IsNullOrEmpty(memberId))
            {
                expression = expression.And(a => a.F_MemberId == memberId);
            }
            if (!string.IsNullOrEmpty(start))
            {
                date = Convert.ToDateTime(start);
                expression = expression.And(a => a.F_ChangeTime >= date);
            }
            if (!string.IsNullOrEmpty(end))
            {
                date = Convert.ToDateTime(end);
                expression = expression.And(a => a.F_ChangeTime <= date);
            }
            List<IntegralDetailEntity> list = integralDetailRepository.FindList(expression, pagination).OrderByDescending(a => a.F_ChangeTime).ToList();

            foreach (var temp in list)
            {
                // 分红人信息
                UserEntity userEntity = userRepository.FindEntity(temp.F_MemberId);
                if (userEntity != null)
                {
                    temp.Account = userEntity.F_Account;
                    temp.RealName = userEntity.F_RealName;
                }
                else
                {
                    temp.Account = "";
                    temp.RealName = "";
                }
            }

            return list;
        }

    }
}